package uppacket

import (
	"time"

	"gitee.com/yanwc/ebicycle/device/frame"
	"gitee.com/yanwc/ebicycle/device/internal/codec"
	"gitee.com/yanwc/ebicycle/device/internal/field"
	"gitee.com/yanwc/ebicycle/device/server"
	"gitee.com/yanwc/ebicycle/device/utils"

	"github.com/panjf2000/gnet"
)

type LoginPacket struct{}

var _ IPacket = (*LoginPacket)(nil)

func (hbp *LoginPacket) Do(frame frame.IFrame, c gnet.Conn) ([]byte,gnet.Action, error) {
	var version field.FirmwareVersion
	var deviceType field.DeviceType
	var imei field.IMEI
	var imsi field.IMSI
	utils.LoadPacket(frame.RawData(), version[:], deviceType[:], imei[:], imsi[:])
	d := server.NewDevice(c, version, imei, imsi, deviceType)
	server.SessionDevices.Add(imei, d)
	timestamp := time.Now().Unix()
	f := &codec.Frame{
		CmdWord: frame.GetCmdWord(),
		SN:      frame.GetSN(),
	}

	return f.WrapPacket(utils.NumberToBytesWithBigEndian[uint32](uint32(timestamp))),gnet.None, nil
}
